//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({
	data: {
		userList: [],
		babyName:"",
		birthDay:"",
		babySex:"1",
		hasUserInfo: false,
		showShadow:false,
		index:0,
		array: ['女','男'],
	},
	addBaby:function(){
		this.setData({
			showShadow: true,
		});
	},
	closeDialog:function(){
		this.setData({
			showShadow: false,
		});
	},
	onLoad: function(options) {
		// 页面初始化 options为页面跳转所带来的参数
		this.setData({
			birthDay: util.formatTime(new Date(),0)
		});
	},
	onShow:function(){
		this.getBobyList(app.globalData.userId);
	},
	getBobyList:function(ownerId){
		let _this = this;
		api.getBobyList({
			data: {
				ownerId: ownerId,
				pageSize: "100",
				pageNum: "1",
			},
			success: function(res) {
				if(res.data.code == 0) {
					res.data.list.rows.forEach(function(item){
						item.birthDay = item.birthDay.substr(0,10);
					});
					_this.setData({
						userList: res.data.list.rows,
					});
				}
			},
		});
	},
	delBaby:function(event){
		let id = event.currentTarget.dataset.id;
		let _this = this;
		api.bobyRemove({
			data: {
				id: id
			},
			success: function(res) {
				if(res.data.code == 0) {
					_this.getBobyList(app.globalData.userId);
				}else{
					wx.showModal({
						 title: '提示',
						 content: "删除失败",
						 showCancel:false,
						 success: function(res) {
						  
						 }				
					});
				}
			},
		});
	},
	bindDateChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			birthDay: e.detail.value
		})
	},
	bindPickerChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			babySex: e.detail.value
		})	
	},
	bobyNameInput:function(e){
		this.setData({
	      babyName: e.detail.value
	    })
	},
	saveBaby:function(e){
		if(e.detail.value.bobyName == ""){
			wx.showModal({
				 title: '提示',
				 content: "请输入宝宝姓名",
				 showCancel:false,
				 success: function(res) {
				  
				 }				
			});
			return false;
		}
		let _this = this;
		api.bobySave({
			data: {
				babySex: e.detail.value.babySex,
				babyName: e.detail.value.bobyName,
				birthDay: e.detail.value.birthDay,
				ownerId:app.globalData.userId
			},
			success: function(res) {
				if(res.data.code == 0) {
					_this.setData({
						showShadow: false
					});
					_this.getBobyList(app.globalData.userId);
				}
			},
			fail: function(res) {
				wx.showToast({
					title: res.data.msg,
					icon: 'success',
					duration: 2000
				})
			}
		});
	}
	
})